home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10914 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.1 KB

  1. Path: snews.tcel.com!netway
  2. From: tech@netway.ab.ca (Ritchie Annand)
  3. Newsgroups: comp.lang.c,comp.lang.c++
  4. Subject: Re: Speed of C program vs. Speed of Pascal program
  5. Date: 10 Mar 1996 13:43:00 GMT
  6. Organization: Telnet Canada (403) 262-5859 info@tcel.com
  7. Message-ID: <4hum94$npg@challenge.tcel.com>
  8. References: <4hsf8p$c5d@caesar.ultra.net>
  9. NNTP-Posting-Host: 204.209.150.60
  10. X-Newsreader: News Xpress Version 1.0 Beta #4
  11.  
  12. In article <4hsf8p$c5d@caesar.ultra.net>,
  13.    kbrady@ultranet.com (Ken Brady) wrote:
  14. >I recently began learning to program in C/C++ after many years
  15. >of using Borland Turbo Pascal (v6.0).  I have been porting a data analysis 
  16. >program into C/C++, and notice that a subroutinefor reading large tables of 
  17. >numbers (ASCII) and loading these numbers into (binary) arrays proceeds much 
  18. >more rapidly in my C program than in my Pascal program.  For one large 
  19. table, 
  20. >my Pascal program requires about 10 s to load the table, while the C program 
  21. >loads it in less than 1 second.
  22. >
  23. >The I/O algorithms in these programs are essentially identical.  I deduce 
  24. >that the C atof function is much faster than the Pascal Val function.  Is 
  25. >this typical?
  26. >
  27. >My C/C++ compiler is Watcom 10.5, running under OS/2.
  28.  
  29. Actually, that depends...
  30.  
  31. It's quite possible atof is faster than Val - they are both library 
  32. functions, after all, but generally speaking, you won't get a 10:1 speed ramp 
  33. between the two from that sort of difference.
  34.  
  35. It'd be my guess that you're using unbuffered Pascal I/O versus buffered C 
  36. I/O... I've found that to be where most file-access differences lie:
  37.  
  38. If your Pascal program uses:
  39. var
  40.   f : Text;
  41.  
  42. Then it's buffered (but not with a very large buffer - you can set it, 
  43. though)
  44.  
  45. If you use:
  46. var
  47.   f : File;
  48. or
  49.   f : File of Char;
  50. or
  51.   f : File of MyRecord;
  52.  
  53. Then it's unbuffered.
  54.  
  55. With f : File, though, you do have the option of doing BlockRead and 
  56. BlockWrite.  Reset(f,SizeOf(MyRecord)) - if you're using a flat file of 
  57. MyRecords, and BlockRead(f,MyRecordArray,NumberOfRecords,ActualRecordsRead) - 
  58. that will give you some mighty good throughput ;)
  59.  
  60.   --=- Ritchie A.
  61.